window.addEventListener('load',function(){

    var num=0;
    var circle=0;
//根据图片数量添加圆圈个数
    var lunbotu=this.document.querySelector('.lunbotu');
    var yuanquan=this.document.querySelector('.yuanquan');
    var you=document.querySelector('.jiaodian');
    var lunbotuWidth=you.offsetWidth;
    console.log(lunbotuWidth);
    for(var i=0;i<lunbotu.children.length;i++){
        li=document.createElement('li');
        //通过自定义属性，记录当前索引号
        li.setAttribute('index',i);
        yuanquan.appendChild(li);
//给点击的圆圈添加类名
        li.addEventListener('click',function(){
            for(var i=0;i<yuanquan.children.length;i++){
                yuanquan.children[i].className='';
            }
            this.className='ys';
            //点击小圆圈移动图片
            var index=this.getAttribute('index');
            num=index;
            circle=index;
            
            donghua(lunbotu,-index*lunbotuWidth);
        })

    }

    yuanquan.children[0].className='ys';

//克隆第一张图片
var first=lunbotu.children[0].cloneNode(true);
lunbotu.appendChild(first);
//通过按钮移动图片
var zuohua=this.document.querySelector('#zuohua');
var youhua=this.document.querySelector('#youhua');
//节流阀
var flog=true;
//给右侧按钮注册事件
youhua.addEventListener('click',function(){
    if(flog==true)
    {
        flog=false;
        if(num==lunbotu.children.length-1){
            lunbotu.getElementsByClassName.left=0;
            num=0;
        }
       num++;
       donghua(lunbotu,-num*lunbotuWidth,function(){
           flog=true;
       });
       circle++;
       if(circle==yuanquan.children.length){
           circle=0;
       }
       paita();
    }
    
});


//给左侧按钮注册事件
zuohua.addEventListener('click',function(){
    if(flog==true){

        flog=false;
        if(num==0){
            num=lunbotu.children.length-1;
            lunbotu.getElementsByClassName.left=-num*lunbotuWidth+'px';
            
        }
       num--;
       donghua(lunbotu,-num*lunbotuWidth,function(){
           flog=true;
       });
       circle--;
       if(circle<0){
           circle=yuanquan.children.length-1;
       }
    
    
       paita();
       
    }
    
});


function paita(){
    for(var i=0;i<yuanquan.children.length;i++){
        yuanquan.children[i].className='';
       }
       yuanquan.children[circle].className='ys';
   }
  

//自动播放轮播图
var timer=setInterval(function(){
//手动调用点击事件
youhua.click();
},2000);

you.addEventListener('mouseenter',function(){
    clearInterval(timer);
    timer=null;
})
you.addEventListener('mouseleave',function(){
    timer=setInterval(function(){
        //手动调用点击事件
        youhua.click();
        },2000);
})

})


            
  